package com.qfedu.mtl.dao.impl;

import com.qfedu.mtl.dao.GoodDao;
import com.qfedu.mtl.entity.Good;
import com.qfedu.mtl.query.GoodQuery;
import com.qfedu.mtl.util.DBUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.handler.impl.BeanHandler;
import com.qfedu.mtl.util.handler.impl.BeanListHandler;
import com.qfedu.mtl.util.handler.impl.NumberHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2023/9/26 19:09
 */
public class GoodDaoImpl implements GoodDao {
    @Override
    public List<Good> selectData(Integer page, Integer size, GoodQuery query) {
        List<Object> params=new ArrayList<>();
        StringBuilder sqlBuilder=new StringBuilder("select * from tb_good where 1=1");
        if (!StrUtil.isNullOrEmpty(query.getGoodName())){
            sqlBuilder.append(" and good_name = ? ");
            params.add(query.getGoodName());
        }
        sqlBuilder.append(" limit ?,?");
        params.add((page-1)*size);
        params.add(size);
        return DBUtil.executeDQL(sqlBuilder.toString(),DBUtil.getConn(),new BeanListHandler<>(Good.class,true),params.toArray());

    }

    @Override
    public Integer selectTotal(GoodQuery query) {
        List<Object> params=new ArrayList<>();
        StringBuilder sqlBuilder=new StringBuilder("select count(*) from tb_good where 1=1");
        if (!StrUtil.isNullOrEmpty(query.getGoodName())){
            sqlBuilder.append(" and good_name = ? ");
            params.add(query.getGoodName());
        }

        return DBUtil.executeDQL(sqlBuilder.toString(),DBUtil.getConn(),new NumberHandler<>(Long.class),params.toArray()).intValue();
    }

    @Override
    public Good selectById(Integer id) {
        String sql="select * from tb_good where good_id = ?";
        return DBUtil.executeDQL(sql,DBUtil.getConn(),new BeanHandler<>(Good.class,true),id);
    }

    @Override
    public int insert(Good good) {
        String sql="insert into tb_good(good_name,good_img,good_cost,good_min_price,good_first_price,good_second_price," +
                "good_third_price,good_forth_price,good_fifth_price)values(?,?,?,?,?,?,?,?,?)";
        return DBUtil.executeDML(sql,DBUtil.getConn(),good.getGoodName(),good.getGoodImg(),good.getGoodCost(),
        good.getGoodMinPrice(),good.getGoodFifthPrice(),good.getGoodSecondPrice(),good.getGoodThirdPrice(),
        good.getGoodForthPrice(),good.getGoodFifthPrice());
    }

//good_name,good_img,good_cost,good_min_price,good_first_price,good_second_price,good_third_price,good_forth_price,good_fifth_price


    @Override
    public int delete(Integer id) {
        String sql="delete from tb_good where good_id = ?";
        return DBUtil.executeDML(sql,DBUtil.getConn(),id);
    }

    @Override
    public int deleteBatch(List<Integer> ids) {
        String sql="delete from tb_good where good_id in (";
        for (int i = 0; i < ids.size(); i++) {
            if (i!= (ids.size()-1)){
                sql+="?,";
            }else {
                sql+="?)";
            }
        }
        return DBUtil.executeDML(sql,DBUtil.getConn(),ids.toArray());
    }

    @Override
    public int update(Good good) {
        String sql="update tb_good set good_name=?,good_img=?,good_cost=?,good_min_price=?,good_first_price=?,good_second_price=?," +
                "good_third_price=?,good_forth_price=?,good_fifth_price=? where id = ? ";
        return DBUtil.executeDML(sql,DBUtil.getConn(),good.getGoodName(),good.getGoodImg(),good.getGoodCost(),
                good.getGoodMinPrice(),good.getGoodFifthPrice(),good.getGoodSecondPrice(),good.getGoodThirdPrice(),
                good.getGoodForthPrice(),good.getGoodFifthPrice(),good.getGoodId());
    }
}
